#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Define Image Parameters                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 10/01/2007                                             #
# Last Modification: 10/01/2007                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 25
#
# MANUAL: This script allows to set certain parameters for all selected images.
#
# DISPLAY: tempkeep
# DISPLAY: crop_histogram
# DISPLAY: crop_histogram_percent
# DISPLAY: crop_histogram_stdev
# DISPLAY: RESMAX
# DISPLAY: spotlist_RESMAX
# DISPLAY: do_lattice_algorithm
# DISPLAY: maska
# DISPLAY: maskb01
# DISPLAY: maskb02
# DISPLAY: boxa1
# DISPLAY: boxa2
# DISPLAY: boxb1
# DISPLAY: boxb2
# DISPLAY: quadrada
# DISPLAY: quadradb
# DISPLAY: df_end
# DISPLAY: movie_enable
# DISPLAY: domask
# DISPLAY: movie_masking_mode
# DISPLAY: ctfcor_imode
# DISPLAY: movie_enable
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: movie_imagenumber_toave
#
#$end_local_vars
#
# This defines some variables that we might need:
set proc_2dx = ""
set bin_2dx = ""
#
set tempkeep = ""
set magnification = ""
set crop_histogram = ""
set crop_histogram_percent = ""
set crop_histogram_stdev = ""
set CS = ""
set KV = ""
set RESMAX = ""
set spotlist_RESMAX = ""
set stepdigitizer = ""
set maska = ""
set do_lattice_algorithm = ""
set maskb01 = ""
set maskb02 = ""
set boxa1 = ""
set boxa2 = ""
set boxb1 = ""
set boxb2 = ""
set quadrada = ""
set quadradb = ""
set df_end = ""
set movie_enable = ""
set domask = ""
set movie_imagenumber_toave = ""
set movie_masking_mode = ""
set ctfcor_imode = ""
#
#$end_vars
#
# This sets the scriptname variable:
set scriptname = 2dx_define_params
#
# This removes the old logfile, so that a new fresh one can be created:
\rm -f LOGS/${scriptname}.results
#
# This initializes the ccp4 environment, and other variables.
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
# This advances the progress bar to the 1% position:
echo "<<@progress: 1>>"
#
# This creates all needed subdirectories in the current merge directory, in case they are missing:
set IS_2DX = yes
source ${proc_2dx}/2dx_makedirs 
#
#############################################################################
#
# This translates the list of directories to work on into one single long line:
cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
#
set tmp = `wc -l 2dx_merge_dirfile.dat`
set images_num = `echo ${tmp} | cut -d\  -f1`
set images_increment = `echo ${images_num} | awk '{ s = 80.0 / $1 } END { print s }'`
set images_current = 10.0
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
echo "<<@progress: 10>>"
#
# This initiates a big "foreach" loop that sets the variable "dirfile" to each of the items in the "dirlist" and runs the contents of the loop with this value:
foreach dirfile ( ${dirlist} ) 
  cd ..
  cd ${dirfile}
  set newdir = $PWD
  cd ${olddir}
  echo "::Working on ${newdir}"
  # This tells the 2dx_merge GUI that the following variable output concerns the image "newdir":
  echo "<IMAGEDIR="\"${newdir}\"">" >> LOGS/${scriptname}.results
  set images_progres = `echo ${images_current} | awk '{ s = $1 } END { printf "%.0f", s }'` 
  echo "<<@progress: ${images_progres}>>"
  set images_current = `echo ${images_current} ${images_increment} | awk '{ s = $1 + $2 } END { print s }'`
  #
  ################################################################################
  ######### Modify only the section below ########################################
  ################################################################################
  #
  echo "set tempkeep = ${tempkeep}" >> LOGS/${scriptname}.results
  echo "set crop_histogram = ${crop_histogram}" >> LOGS/${scriptname}.results
  echo "set crop_histogram_percent = ${crop_histogram_percent}" >> LOGS/${scriptname}.results
  echo "set crop_histogram_stdev = ${crop_histogram_stdev}" >> LOGS/${scriptname}.results
  echo "set ctfcor_imode = ${ctfcor_imode}" >> LOGS/${scriptname}.results
  # echo "set magnification = ${magnification}" >> LOGS/${scriptname}.results
  # echo "set CS = ${CS}" >> LOGS/${scriptname}.results
  # echo "set KV = ${KV}" >> LOGS/${scriptname}.results
  echo "set RESMAX = ${RESMAX}" >> LOGS/${scriptname}.results
  # echo "set stepdigitizer = ${stepdigitizer}" >> LOGS/${scriptname}.results
  echo "set do_lattice_algorithm = ${do_lattice_algorithm}" >> LOGS/${scriptname}.results
  echo "set maska = ${maska}" >> LOGS/${scriptname}.results
  echo "set maskb01 = ${maskb01}" >> LOGS/${scriptname}.results
  echo "set maskb02 = ${maskb02}" >> LOGS/${scriptname}.results
  echo "set boxa1 = ${boxa1}" >> LOGS/${scriptname}.results
  echo "set boxa2 = ${boxa2}" >> LOGS/${scriptname}.results
  echo "set boxb1 = ${boxb1}" >> LOGS/${scriptname}.results
  echo "set boxb2 = ${boxb2}" >> LOGS/${scriptname}.results
  echo "set quadrada = ${quadrada}" >> LOGS/${scriptname}.results
  echo "set quadradb = ${quadradb}" >> LOGS/${scriptname}.results
  echo "set spotlist_RESMAX = ${spotlist_RESMAX}" >> LOGS/${scriptname}.results
  echo "set df_end = ${df_end}" >> LOGS/${scriptname}.results
  echo "set movie_enable = ${movie_enable}" >> LOGS/${scriptname}.results
  echo "set domask = ${domask}" >> LOGS/${scriptname}.results
  # echo "set movie_imagenumber_toave = ${movie_imagenumber_toave}" >> LOGS/${scriptname}.results
  echo "set movie_masking_mode = ${movie_masking_mode}" >> LOGS/${scriptname}.results
  #
  ################################################################################
  ######### Modify only the section above ########################################
  ################################################################################
  #
  # This tells 2dx_merge that the output for the image is done:
  echo "</IMAGEDIR>" >> LOGS/${scriptname}.results
end
#
# Move the progress bar to 100%:
echo "<<@progress: 100>>"
#
# Leave a message on the logfile that the script finished:
${proc_2dx}/linblock "${scriptname} finished correctly."
#
